﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Design;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Marmat.Forms.Skin
{

    [ToolboxItem(false)]
    [Designer("System.Windows.Forms.Design.ParentControlDesigner, System.Design", typeof(IDesigner))]
    public partial class TabPage : System.Windows.Forms.UserControl
    {
        internal TabControl parent;
        /// <summary>
        /// 滚动图的扩展支持。
        /// </summary>
        [DebuggerBrowsable(DebuggerBrowsableState.Never)]
        internal protected ScrollableControlExtension scrollableControlExtension;

        public TabPage()
        {
            SetStyle(ControlStyles.Selectable, true);
            SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            SetStyle(ControlStyles.ContainerControl, true);
            base.Dock = DockStyle.Fill;
            this.scrollableControlExtension = new ScrollableControlExtension(this);
        }


        /// <summary>
        /// 获取或设置选项卡的边框颜色。
        /// </summary>
        /// <returns></returns>
        [DefaultValue(typeof(Color), "238, 238, 238")]
        [Browsable(true)]
        [Category("外观")]
        [Description("获取或设置选项卡的边框颜色。")]
        public Color BorderColor { get; set; } = Color.FromArgb(238, 238, 238);

        /// <summary>
        ///     ''' 获取或设置控件的高度和宽度。
        ///     ''' </summary>
        ///     ''' <returns> System.Drawing.Size，表示控件的高度和宽度（以像素为单位）。</returns>
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public new Size Size
        {
            get => base.Size;
            set => base.Size = value;
        }

        /// <summary>
        /// 获取或设置选项卡上的图像。
        /// </summary>
        [DefaultValue(null)]
        public Image Image { get; set; }

        internal ImageList ImageList
        {
            get => m_ImageList;
            set => m_ImageList = value;
        }
        private ImageList m_ImageList;

        /// <summary>
        ///     ''' 获取或设置在该选项卡上显示的图像的索引。
        ///     ''' </summary>
        ///     ''' <returns>中的图像的从零开始索引System.Windows.Forms.TabControl.ImageList，它显示在选项卡。默认值为 -1，表示没有图像。</returns>
        [DefaultValue(-1)]
        [Editor("System.Windows.Forms.Design.ImageIndexEditor, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor))]
        [Localizable(true)]
        [RefreshProperties(RefreshProperties.Repaint)]
        [TypeConverter(typeof(ImageIndexConverter))]
        public int ImageIndex { get; set; } = -1;

        /// <summary>
        ///     ''' 获取或设置在该选项卡作为焦点时显示的图像的索引。
        ///     ''' </summary>
        ///     ''' <returns>中的图像的从零开始索引System.Windows.Forms.TabControl.ImageList，它显示在选项卡。默认值为 -1，表示没有图像。</returns>
        [DefaultValue(-1)]
        [Editor("System.Windows.Forms.Design.ImageIndexEditor, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor))]
        [Localizable(true)]
        [RefreshProperties(RefreshProperties.Repaint)]
        [TypeConverter(typeof(ImageIndexConverter))]
        public int FocusImageIndex { get; set; } = -1;

        /// <summary>
        ///     ''' 获取或设置关联 System.Windows.Forms.TabControl.ImageList 的 System.Windows.Forms.TabControl中的图像的键访问器。
        ///     ''' </summary>
        ///     ''' <returns>一个表示图像的键的字符串。</returns>
        [DefaultValue("")]
        [Editor("System.Windows.Forms.Design.ImageIndexEditor, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor))]
        [Localizable(true)]
        [RefreshProperties(RefreshProperties.Repaint)]
        [TypeConverter(typeof(ImageKeyConverter))]
        public string ImageKey { get; set; } = "";

        /// <summary>
        ///     ''' 获取或设置关联 System.Windows.Forms.TabControl.ImageList 的 System.Windows.Forms.TabControl中的图像的键访问器。
        ///     ''' </summary>
        ///     ''' <returns>一个表示图像的键的字符串。</returns>
        [DefaultValue("")]
        [Editor("System.Windows.Forms.Design.ImageIndexEditor, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor))]
        [Localizable(true)]
        [RefreshProperties(RefreshProperties.Repaint)]
        [TypeConverter(typeof(ImageKeyConverter))]
        public string FocusImageKey { get; set; } = "";

        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public new Point Location
        {
            get => base.Location;
            set => base.Location = value;
        }

        [DefaultValue(typeof(DockStyle), "Fill")]
        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public override DockStyle Dock
        {
            get => base.Dock;
            set => base.Dock = value;
        }

        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public new bool Visible
        {
            get => base.Visible;
            set => base.Visible = value;
        }

        /// <summary>
        /// 获取或设置tab页面是否可见。
        /// </summary>
        [DefaultValue(true)]
        [Browsable(true)]
        public bool TabVisible
        {
            get => this.m_TabVisible;
            set
            {
                if (this.m_TabVisible != value)
                {
                    if (!this.DesignMode)
                        base.Visible = value;
                    this.m_TabVisible = value;
                    this.parent?.Invalidate();
                }
            }
        }

        private bool m_TabVisible = true;

        /// <summary>
        /// 获取或设置与此控件关联的文本。
        /// </summary>
        /// <returns>与此控件关联的文本。</returns>
        [Browsable(true)]
        [EditorBrowsable(EditorBrowsableState.Always)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public override string Text
        {
            get => base.Text;
            set => base.Text = value;
        }

        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public new int TabIndex
        {
            get
            {
                return base.TabIndex;
            }
            set
            {
                base.TabIndex = value;
            }
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            if (this.DesignMode)
            {
                Pen pen = new Pen(Color.Gray);
                pen.Width = 1;
                pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
                e.Graphics.DrawRectangle(pen, new Rectangle(1, 1, this.Width - 3, this.Height - 3));
            }
        }

        protected override void WndProc(ref Message m)
        {
            if (this.scrollableControlExtension.BeforeWndProc(ref m, out var arguments))
                base.WndProc(ref m);
            this.scrollableControlExtension.AfterWndProc(m, arguments);
        }
    }
}
